package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;


//录入建议信息
//完成与成绩信息的比对工作
//当成绩信息中有相似信息时会跳转到overwrite

public class judge extends HttpServlet {

    public static String Name;
    public static String course;
    public static String Advice;

    private static final String INSERT_TEMPLATE =
            "INSERT INTO courses (`nam`, `course`, `advice`) " +
                    "VALUES ('%s', '%s', '%s')";


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String name = request.getParameter("nam");
        String subject = request.getParameter("course");
        String advice = request.getParameter("advice");

        Name = name;
        course = subject;
        Advice = advice;

        String sql = " select *  from grade where nam = '" + name + "' and subje ='" + subject + "'";

        String sql06 = "SELECT * FROM `grade` WHERE `nam` = '" + name + "' and `subje` LIKE '%" + subject + "%'";

        System.out.println(sql);


        try {
            List<Student> students = DBMS.getStudents(sql);
            if (students.isEmpty())                      //检查是否成绩信息为空，这里检查的信息是完整的，科目和姓名必须完整
            {
                System.out.println(sql06);                //如果成绩信息为空，检查是不是没有输入完整
                List<Student> students02 = DBMS.getStudents(sql06);
                if (students02.isEmpty()) {               //模糊搜索也没有找到，就提升进行输入
                    Writer writer = response.getWriter();
                    StringBuilder sb = new StringBuilder();
                    sb.append(Page.getHeader());
                    sb.append(toHtml02());
                    sb.append(Page.getEnd());
                    writer.write(sb.toString());
                } else {                                  //模糊搜索找到，提升使用者进行判断
                    response.setCharacterEncoding("UTF-8");
                    Writer writer = response.getWriter();
                    StringBuilder sb =new StringBuilder();

                    sb.append(Page.getHeader());
                    sb.append(toHtml03(students02));
                    sb.append(Page.getEnd());
                    writer.write(sb.toString());


                }
            } else {                                      //在成绩信息中找到了完整的信息进行成绩的判断
                response.setCharacterEncoding("UTF-8");

                Student student = students.get(0);
                int score = student.getScore();

                if (score >= 85) {                         //大于等于85分，可以进行写入
                    String sql02 = String.format(INSERT_TEMPLATE, Name, course, Advice);
                    System.out.println(sql02);
                    DBMS.insert(sql02);
                    response.sendRedirect("./listExperience");
                } else {                                  //小于85分，进行提示

                    Writer writer = response.getWriter();
                    StringBuilder sb = new StringBuilder();

                    sb.append(Page.getHeader());
                    sb.append(toHtml());
                    sb.append(Page.getEnd());
                    writer.write(sb.toString());
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String toHtml() {
        StringBuilder sb = new StringBuilder();
        sb.append("<center>");
        sb.append("<br><br><br>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">添加专业的学习建议需要取得该科目85分以上的成绩</font><br/>");
        sb.append("<br><br><br>");
        sb.append("<font face=\"宋体\" size=\"7\" color=\"#ff4d00\">感谢您的支持</font><br/>");
        sb.append("<font face=\"宋体\" size=\"7\" color=\"#ff4d00\">期待您的下次分享</font><br/>");
        sb.append("</center>");
        return sb.toString();

    }

    public String toHtml02() {
        StringBuilder sb = new StringBuilder();
        sb.append("<center>");
        sb.append("<br><br><br>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">没有在成绩信息中找到您的成绩</font><br/>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">添加专业的学习建议需要先添加该课程的成绩信息哦</font><br/>");
        sb.append("<br><br>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">如已添加，请检查姓名或科目名是否填写正确</font><br/>");
        sb.append("<font face=\"宋体\" size=\"6\" color=\"#ff4d00\">感谢您的支持</font><br/>");
        sb.append("</center>");
        return sb.toString();

    }

    public String toHtml03(List<Student> students) {
        StringBuilder sb =new StringBuilder();
        sb.append("<center>");
        sb.append("<br><br><br>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">您的建议学科是否是以下信息呢？</font><br/>");
        sb.append("<br>");
        sb.append("<font face=\"宋体\" size=\"5\" color=\"#003153\">姓名和科目记得要输入完整哦</font><br/>");
        sb.append("<br><br>");
        sb.append("<font face=\"宋体\" size=\"6\" color=\"#ff4d00\">感谢您的支持</font><br/>");
        sb.append("</center>");
        sb.append("<center><br><table style='width:65%' border='0' cellpadding='10'>");
        sb.append("<tr style='background-color:#747474;color:#ff5400'>" +
                "<th>编号</th><th>学号</th><th>照片</th><th>名字</th><th>科目</th><th>分数</th><th>感想</th>" +
                "<th></th></tr>");
        for (int i=0; i<students.size(); i++) {
            Student student =students.get(i);
            if (i % 6 == 0) {
                sb.append("<tr style='background-color:#f0e68c'>");
            } else if(i % 6==1){
                sb.append("<tr style='background-color:#ffff99'>");
            }else if(i % 6==2){
                sb.append("<tr style='background-color:#30d5c8'>");
            }else if(i % 6==3){
                sb.append("<tr style='background-color:#0dbf8c'>");
            }else if(i % 6==4){
                sb.append("<tr style='background-color:#faf0e6'>");
            }else if(i % 6==5){
                sb.append("<tr style='background-color:#eac096'>");
            }
            sb.append("<td>").append(student.getID()).append("</td>");
            sb.append("<td><b>").append(student.getNumber()).append("</b></td>");

            sb.append("<td><img width='100px' src='/gittest/upload/").append(student.getPic()).append("'></img></td>");

            sb.append("<td>").append(student.getName()).append("</td>");
            sb.append("<td>").append(student.getSubject()).append("</td>");
            sb.append("<td>").append(student.getScore()).append("</td>");
            sb.append("<td>").append(student.getFeel()).append("</td>");
            sb.append("<td>").append(
                    String.format("<a href='./overwrite?id=%s'>点击添加学习建议</a>", student.getID())
            ).append("</td>");
            sb.append("</tr>");
        }
        sb.append("</table></center>");
        return sb.toString();

        }

}

